package pl.lodz.p.kipme;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.jws.WebService;

import pl.lodz.p.kipme.bo.Category;
import pl.lodz.p.kipme.bo.Order;
import pl.lodz.p.kipme.bo.Product;
import pl.lodz.p.kipme.bo.ProductInfo;
import pl.lodz.p.kipme.bo.ProductOrder;
import pl.lodz.p.kipme.bo.ProductType;
import pl.lodz.p.kipme.bo.Transaction;
import pl.lodz.p.kipme.exception.BusinessException;

@WebService(endpointInterface = "pl.lodz.p.kipme.MainService", serviceName = "mainService")
public class MainServiceImpl implements MainService {
    private List<ProductType> prodTypeList = new ArrayList<ProductType>();
    private List<Category> categoryList = new ArrayList<Category>();
    private List<Product> productList = new ArrayList<Product>();
    private HashMap<String, List<Product>> catMap = new HashMap<String, List<Product>>();
    private HashMap<String, List<Product>> typeMap = new HashMap<String, List<Product>>();
    
    public MainServiceImpl(){
        initialValues();
    }
    
    public List<Category> getCategoryList() {

        

        return categoryList;
    }

    public List<ProductType> getProductTypeList() {

        
        return prodTypeList;
    }

    public List<Product> getProductsByCategory(String categoryCode) {


        return catMap.get(categoryCode);
    }

    public List<Product> getProductsByType(String typeCode) {

        return typeMap.get(typeCode);
    }

    public List<ProductInfo> getProductInfo() {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        for(Product product : productList){
            ProductInfo info = new ProductInfo();
            info.setName(product.getName());
            info.setProductCode(product.getProductCode());
            list.add(info);
        }

        return list;
    }

    public void reportSale(Transaction transaction) {
        // TODO: zapis do bazy
    }

    private void initialValues() {
        Category category = new Category();
        category.setCategoryCode("horror");
        category.setName("Horror");
        categoryList.add(category);
        Category category2 = new Category();
        category2.setCategoryCode("family");
        category2.setName("Without age restrict");
        categoryList.add(category2);
        Category category3 = new Category();
        category3.setCategoryCode("teenage");
        category3.setName("For teenages");
        categoryList.add(category3);

        ProductType type = new ProductType();
        type.setName("Book");
        type.setTypeCode("book");
        prodTypeList.add(type);
        ProductType type2 = new ProductType();
        type2.setName("Audio CD");
        type2.setTypeCode("cd");
        prodTypeList.add(type2);
        ProductType type3 = new ProductType();
        type3.setName("Movie od DVD");
        type3.setTypeCode("dvd");
        prodTypeList.add(type3);
        ProductType type4 = new ProductType();
        type4.setName("Magazine");
        type4.setTypeCode("mag");
        prodTypeList.add(type4);
        
        Product product = new Product();
        product.setProductCode("1A");
        product.setName("Dan Brown - Leonardo Code");
        product.setDescribe("Bestseller on world's lists");
        product.setPrice(37.99f);
        product.setCategory(category3);
        product.setType(type);
        productList.add(product);
        
        Product product2 = new Product();
        product2.setProductCode("1B");
        product2.setName("Stephen King - Carrie");
        product2.setDescribe("One of the best modern horrors");
        product2.setPrice(34.99f);
        product2.setCategory(category);
        product2.setType(type);
        productList.add(product2);
        
        Product product3 = new Product();
        product3.setProductCode("1C");
        product3.setName("National Geographic");
        product3.setDescribe("Amazing photos and reports from wild travels");
        product3.setPrice(15.49f);
        product3.setCategory(category2);
        product3.setType(type4);
        productList.add(product3);
        
        Product product4 = new Product();
        product4.setProductCode("1D");
        product4.setName("Titanic");
        product4.setDescribe("Wonderful story about love and sacrifice");
        product4.setPrice(57.99f);
        product4.setCategory(category2);
        product4.setType(type3);
        productList.add(product4);
        
        Product product5 = new Product();
        product5.setProductCode("1E");
        product5.setName("Mozart - Eine kleine nacht music");
        product5.setDescribe("Masterpiece of worlds music");
        product5.setPrice(42.99f);
        product5.setCategory(category2);
        product5.setType(type2);
        productList.add(product5);
        
        Product product6 = new Product();
        product6.setProductCode("1F");
        product6.setName("Harry Potter and Deathly Hallows vol.2");
        product6.setDescribe("Dark end of great magic series");
        product6.setPrice(55.89f);
        product6.setCategory(category3);
        product6.setType(type3);
        productList.add(product6);
        
        List<Product> l1 = new ArrayList<Product>();
        List<Product> l2 = new ArrayList<Product>();
        List<Product> l3 = new ArrayList<Product>();
        l1.add(product6);
        l1.add(product);
        l1.add(product2);
        l1.add(product3);
        l1.add(product4);
        l1.add(product5);
        catMap.put("teenage", l1);
        catMap.put("horror", l2);
        catMap.put("family", l3);
        

        List<Product> l4 = new ArrayList<Product>();
        List<Product> l5 = new ArrayList<Product>();
        List<Product> l6 = new ArrayList<Product>();
        List<Product> l7 = new ArrayList<Product>();
        l6.add(product6);
        l6.add(product4);
        l4.add(product);
        l4.add(product2);
        l7.add(product3);
        l5.add(product5);
        
        typeMap.put("book", l4);
        typeMap.put("cd", l5);
        typeMap.put("dvd", l6);
        typeMap.put("mag", l7);
        
    }

    public Product getProductByProductCode(String productCode) {
        for (Product product : productList) {
            if(product.getProductCode().endsWith(productCode)){
                return product;
            }
        }
        return new Product();
    }


    @Override
    public List<ProductOrder> checkOrderStatus(String orderId) {
        return null;
    }

    @Override
    public List<String> getActiveOrderId() {
        return null;
    }

    @Override
    public void confirmProductReceive(String productOrderId) {
    }

    @Override
    public void updateNumberOfProduct(String productCode, String newAmount) {
    }

    @Override
    public int getNumberOfProduct(String productCode) {
        return 0;
    }

    @Override
    public List<Transaction> getTransactions(Date startDate, Date endDate) {
        return null;
    }

    @Override
    public List<Transaction> getTransactionsByProductCode(String productCode) {
        return null;
    }

	@Override
	public void addProduct(Product product) throws BusinessException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateProduct(Product product) throws BusinessException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeProduct(String productCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void placeOrder(Order order) throws BusinessException {
		// TODO Auto-generated method stub
		
	}

}
